﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;
using System.Text;

public partial class DeliverGoodsManagement_ScanDelivery : System.Web.UI.Page, ICallbackEventHandler
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (!Check.CheckPermission("500100", "00014"))
            {
                ScriptManager.RegisterStartupScript(this, this.GetType(), "StartUp", "alert('无权操作')", true);
                return;
            }
        }
    }

    #region ICallbackEventHandler 成员
    string result = "";
    public string GetCallbackResult()
    {
        return result;
    }

    public void RaiseCallbackEvent(string eventArgument)
    {
        string[] arr = eventArgument.Split('|');
        Model.PICI pc = new BLL.PICI().GetModel(Share.UserValue(0), "PCH_ID");
        if (arr[0] == "Order")
        {
            result = "<table cellspacing=\"0\" rules=\"all\" border=\"1\" id=\"grdOrderDetail\" style=\"width:100%;border-collapse:collapse;\"><tr><th scope=\"col\">订单编号</th><th scope=\"col\">商品代码</th><th scope=\"col\">商品名称</th><th scope=\"col\">颜色代码</th><th scope=\"col\">颜色</th><th scope=\"col\">尺码代码</th><th scope=\"col\">尺码</th><th scope=\"col\">数量</th><th scope=\"col\">扫描数量</th><th scope=\"col\">商品条码</th></tr>";


            string str = string.Format(@"SELECT OD_ID,OD_CMDCODE,SPMC,OD_COLORCODE,GUIGE1.GGMC AS COLORNAME,OD_SIZECODE,GUIGE2.GGMC AS SIZENAME,OD_QTY,0 AS SCANQTY,TMDZB.SPTM,OD_DETAILID
                                                            FROM ORDERDETAIL 
                                                            LEFT JOIN TMDZB ON OD_CMDCODE=TMDZB.SPDM AND OD_COLORCODE=TMDZB.GG1DM AND OD_SIZECODE=TMDZB.GG2DM
                                                            LEFT JOIN SHANGPIN ON SHANGPIN.SPDM=OD_CMDCODE
                                                            LEFT JOIN GUIGE1 ON GUIGE1.GGDM=OD_COLORCODE
                                                            LEFT JOIN GUIGE2 ON GUIGE2.GGDM=OD_SIZECODE WHERE OD_ID='{0}'", arr[1]);
            DataSet ds = new BLL.ORDERDOC().GetDataSet(str);

            foreach (DataRow row in ds.Tables[0].Rows)
            {
                result = result + "<tr><td id='order'>"+row["OD_ID"].ToString()+"</d><td id='code'>" + row["OD_CMDCODE"].ToString() + "</td><td id='mc'>" + row["SPMC"].ToString() + "</td><td id='color'>" + row["OD_COLORCODE"].ToString() + "</td><td id='colorname'>" + row["COLORNAME"].ToString() + "</td><td id='size'>" + row["OD_SIZECODE"].ToString() + "</td><td id='sizename'>" + row["SIZENAME"].ToString() + "</td><td id='qty'>" + row["OD_QTY"].ToString() + "</td><td id='sqty'>" + row["SCANQTY"].ToString() + "</td><td id='tm'>" + row["SPTM"].ToString() + "</td><td id='id' style='display:none'>" + row["OD_DETAILID"].ToString() + "</td>";
            }

            result = result + "</table>|Order";
        }
        else
        {
            if (arr[0] == "Check")
            {
                DataSet ds = null;
                string ret = new BLL.ORDERDOC().GetOrderByWhere(string.Format(" where ORDER_TRANBILLCODE ='{0}' and ORDER_ID not in('{1}') ", arr[2], arr[1]), out ds);
                if (ds.Tables[0].Rows.Count > 0)
                {
                    result = "物流单号不允许重复！" + "|";
                }
                else
                {
 
                }

                int pageCount, recordCount;
                Model.ORDERDOC order = new BLL.ORDERDOC().GetModel(arr[1]);
                List<Model.ORDERDETAIL> orderDetails = new BLL.ORDERDETAIL().GetList(100, 1, " OD_DETAILID ", false, " and OD_ID='" + arr[1] + "' ", out pageCount, out recordCount);
                IList<Model.PAYDETAIL> payDetails = new SQLServerDAL.PAYDETAIL().GetPayDetailById(arr[1]);

                if (pc == null)
                {
                    result = result + "请先开始一个批次！" + "|";
                }
                //if (order != null && orderDetails != null && payDetails != null)
                //{
                    
                    if (order.ORDER_SEND == 1)
                    {
                        result = result + "该订单已发货!" + "|";
                    }

                    if (orderDetails.Count == 0)
                    {
                        result = result + "系统无法查询到该订单的明细！" + "|";
                    }

                    if (order.ORDER_APAY != 1)
                    {
                        if (payDetails.Count == 0)
                        {
                            result = result + "系统无法查询到该订单的支付明细！" + "|";
                        }
                    }

                    if (order.ORDER_APAY == 0 && order.ORDER_SECDEAL == 0 && order.ORDER_CLEAR == 0)
                    {
                        result = result + "该订单未结算" + "|";
                    }

                    if (order.ORDER_CHECK != 1 || order.ORDER_ACHECK != 1 || order.ORDER_DELETE != 0)
                    {
                        result = result + "订单不符合发货条件！" + "|";
                    }

                    Model.V_VIPSET userInfo = new BLL.V_VIPSET().GetModel(order.ORDER_CUSTOMERCODE);
                    if (userInfo == null)
                    {
                        result = result + "系统内无相关会员信息！";
                    }
               // }
            }
            else
            { //出库 参数串：Send,objOrder.value,objWLDH.value,1|code,mc,color,size,qty,sqty,tm,id;code,mc,color,size,qty,sqty,tm,id
                int pageCount, recordCount;
                bool ret = false;

                string[] arrMainInfo = arr[0].Split(',');//Send,objOrder.value,objWLDH.value,1
                string[] arrDetails = arr[1].Split(';');//code,mc,color,size,qty,sqty,tm,id;code,mc,color,size,qty,sqty,tm,id

                Model.ORDERDOC order = new BLL.ORDERDOC().GetModel(arrMainInfo[1]);


                //找到要删除的商品明细
                string DeletedOrderDetailID = "";

                for (int i = 0; i < arrDetails.Length - 1; i++)
                {//code,mc,color,size,qty,sqty,tm,id
                    string[] arrDetail = arrDetails[i].Split(',');

                    if (arrDetail[5] == "0")
                    { DeletedOrderDetailID = DeletedOrderDetailID + arrDetail[7] + ","; }
                }

                if (arrMainInfo[3] == "1")
                {//生成差异订单 
                    List<Model.ORDERDETAIL> orderDetails;

                    if (DeletedOrderDetailID != "")
                    {
                        char[] charToTrim = new char[] { ',' };
                        orderDetails = new BLL.ORDERDETAIL().GetList(100, 1, " OD_DETAILID ", false, " and OD_ID='" + order.ORDER_ID + "' and OD_DETAILID not in(" + DeletedOrderDetailID.TrimEnd(charToTrim) + ")", out pageCount, out recordCount);
                    }
                    else
                    {
                        orderDetails = new BLL.ORDERDETAIL().GetList(100, 1, " OD_DETAILID ", false, " and OD_ID='" + order.ORDER_ID + "'", out pageCount, out recordCount);
                    }
                    if (orderDetails != null)
                    {

                        List<Model.ORDERDETAIL> newOrderDetails = new List<Model.ORDERDETAIL>();
                        Model.ORDERDETAIL newOrderDetail;
                        List<Model.ORDERDETAIL> toUpdateOrderDetails = new List<Model.ORDERDETAIL>();
                        decimal quantity;
                        string notes;
                        decimal points;
                        decimal weight;
                        foreach (Model.ORDERDETAIL tempDetail in orderDetails)
                        {
                            for (int i = 0; i < arrDetails.Length - 1; i++)
                            {
                                string[] arrDetail = arrDetails[i].Split(',');
                                if (tempDetail.OD_DETAILID == arrDetail[7])
                                {
                                    //code,mc,color,size,qty,sqty,tm,id
                                    quantity = decimal.Parse(arrDetail[5]);
                                    notes = tempDetail.OD_NOTE;
                                    points = tempDetail.OD_ITG;
                                    weight = tempDetail.OD_WEIGHT;
                                    if (quantity < tempDetail.OD_QTY && quantity > 0)
                                    {
                                        newOrderDetail = new Model.ORDERDETAIL();
                                        newOrderDetail.OD_ID = tempDetail.OD_ID;

                                        newOrderDetail.OD_ID = tempDetail.OD_ID;
                                        newOrderDetail.OD_DETAILID = tempDetail.OD_DETAILID;
                                        newOrderDetail.OD_CMDCODE = tempDetail.OD_CMDCODE;
                                        newOrderDetail.OD_COLORCODE = tempDetail.OD_COLORCODE;
                                        newOrderDetail.OD_SIZECODE = tempDetail.OD_SIZECODE;
                                        newOrderDetail.OD_QTY = tempDetail.OD_QTY - quantity;
                                        newOrderDetail.OD_SENDQTY = tempDetail.OD_SENDQTY;
                                        newOrderDetail.OD_SPRICE = tempDetail.OD_SPRICE;
                                        newOrderDetail.OD_DISC = tempDetail.OD_DISC;
                                        newOrderDetail.OD_APRICE = tempDetail.OD_APRICE;
                                        newOrderDetail.OD_SAMT = newOrderDetail.OD_QTY * newOrderDetail.OD_SPRICE;
                                        newOrderDetail.OD_AAMT = newOrderDetail.OD_QTY * newOrderDetail.OD_APRICE;
                                        newOrderDetail.OD_NOTE = notes;
                                        newOrderDetail.OD_FIELD1 = tempDetail.OD_FIELD1;
                                        newOrderDetail.OD_FIELD2 = tempDetail.OD_FIELD2;
                                        newOrderDetail.OD_SPPRICE = tempDetail.OD_SPPRICE;
                                        newOrderDetail.OD_GIFT = tempDetail.OD_GIFT;
                                        newOrderDetail.OD_ITGBUY = tempDetail.OD_ITGBUY;
                                        newOrderDetail.OD_ITGPRICE = tempDetail.OD_ITGPRICE;
                                        newOrderDetail.OD_ITG = ((tempDetail.OD_QTY - quantity) / tempDetail.OD_QTY) * tempDetail.OD_ITG;
                                        newOrderDetail.OD_ADDITG = tempDetail.OD_ADDITG;
                                        newOrderDetail.OD_WEIGHT = weight;
                                        newOrderDetail.OD_OFFER = tempDetail.OD_OFFER;
                                        newOrderDetail.OD_OFFERTYPE = tempDetail.OD_OFFERTYPE;
                                        newOrderDetail.OD_OFFERID = tempDetail.OD_OFFERID;
                                        newOrderDetail.OD_OFFERNO = tempDetail.OD_OFFERNO;
                                        newOrderDetails.Add(newOrderDetail);

                                        tempDetail.OD_QTY = quantity;
                                        tempDetail.OD_AAMT = tempDetail.OD_APRICE * quantity;
                                        tempDetail.OD_SAMT = tempDetail.OD_SPRICE * quantity;
                                        tempDetail.OD_NOTE = notes;
                                        tempDetail.OD_ITG = points;
                                        tempDetail.OD_WEIGHT = weight;
                                        toUpdateOrderDetails.Add(tempDetail);
                                    }

                                }
                            }
                        }

                        ret = new BLL.ORDERDETAIL().UpdateOrderDetailAndOrderMain(true, order.ORDER_ID, DeletedOrderDetailID, newOrderDetails, toUpdateOrderDetails, Share.UserValue(0), ConfigurationManager.AppSettings["ckdm"].ToString(), ConfigurationManager.AppSettings["kwdm"].ToString(),order.ORDER_TRANERCODE,arrMainInfo[2]);

                    }
                }
                else
                {//修改原订单 
                    List<Model.ORDERDETAIL> orderDetails;

                    if (DeletedOrderDetailID != "")
                    {
                        char[] charToTrim = new char[] { ',' };
                        orderDetails = new BLL.ORDERDETAIL().GetList(100, 1, " OD_DETAILID ", false, " and OD_ID='" + order.ORDER_ID + "' and OD_DETAILID not in(" + DeletedOrderDetailID.TrimEnd(charToTrim) + ")", out pageCount, out recordCount);
                    }
                    else
                    {
                        orderDetails = new BLL.ORDERDETAIL().GetList(100, 1, " OD_DETAILID ", false, " and OD_ID='" + order.ORDER_ID + "'", out pageCount, out recordCount);
                    }
                    if (orderDetails != null)
                    {
                        List<Model.ORDERDETAIL> newOrderDetails = new List<Model.ORDERDETAIL>();
                        Model.ORDERDETAIL newOrderDetail;
                        List<Model.ORDERDETAIL> toUpdateOrderDetails = new List<Model.ORDERDETAIL>();
                        decimal quantity;
                        string notes;
                        decimal points;
                        decimal weight;
                        foreach (Model.ORDERDETAIL tempDetail in orderDetails)
                        {
                            for (int i = 0; i < arrDetails.Length - 1; i++)
                            {
                                string[] arrDetail = arrDetails[i].Split(',');
                                if (tempDetail.OD_DETAILID == arrDetail[7])
                                {
                                    //code,mc,color,size,qty,sqty,tm,id
                                    quantity = decimal.Parse(arrDetail[5]);
                                    notes = tempDetail.OD_NOTE;
                                    points = tempDetail.OD_ITG;
                                    weight = tempDetail.OD_WEIGHT;
                                    if (quantity < tempDetail.OD_QTY && quantity > 0)
                                    {
                                        newOrderDetail = new Model.ORDERDETAIL();
                                        newOrderDetail.OD_ID = tempDetail.OD_ID;

                                        newOrderDetail.OD_ID = tempDetail.OD_ID;
                                        newOrderDetail.OD_DETAILID = tempDetail.OD_DETAILID;
                                        newOrderDetail.OD_CMDCODE = tempDetail.OD_CMDCODE;
                                        newOrderDetail.OD_COLORCODE = tempDetail.OD_COLORCODE;
                                        newOrderDetail.OD_SIZECODE = tempDetail.OD_SIZECODE;
                                        newOrderDetail.OD_QTY = tempDetail.OD_QTY - quantity;
                                        newOrderDetail.OD_SENDQTY = tempDetail.OD_SENDQTY;
                                        newOrderDetail.OD_SPRICE = tempDetail.OD_SPRICE;
                                        newOrderDetail.OD_DISC = tempDetail.OD_DISC;
                                        newOrderDetail.OD_APRICE = tempDetail.OD_APRICE;
                                        newOrderDetail.OD_SAMT = newOrderDetail.OD_QTY * newOrderDetail.OD_SPRICE;
                                        newOrderDetail.OD_AAMT = newOrderDetail.OD_QTY * newOrderDetail.OD_APRICE;
                                        newOrderDetail.OD_NOTE = notes;
                                        newOrderDetail.OD_FIELD1 = tempDetail.OD_FIELD1;
                                        newOrderDetail.OD_FIELD2 = tempDetail.OD_FIELD2;
                                        newOrderDetail.OD_SPPRICE = tempDetail.OD_SPPRICE;
                                        newOrderDetail.OD_GIFT = tempDetail.OD_GIFT;
                                        newOrderDetail.OD_ITGBUY = tempDetail.OD_ITGBUY;
                                        newOrderDetail.OD_ITGPRICE = tempDetail.OD_ITGPRICE;
                                        newOrderDetail.OD_ITG = ((tempDetail.OD_QTY - quantity) / tempDetail.OD_QTY) * tempDetail.OD_ITG;
                                        newOrderDetail.OD_ADDITG = tempDetail.OD_ADDITG;
                                        newOrderDetail.OD_WEIGHT = weight;
                                        newOrderDetail.OD_OFFER = tempDetail.OD_OFFER;
                                        newOrderDetail.OD_OFFERTYPE = tempDetail.OD_OFFERTYPE;
                                        newOrderDetail.OD_OFFERID = tempDetail.OD_OFFERID;
                                        newOrderDetail.OD_OFFERNO = tempDetail.OD_OFFERNO;
                                        newOrderDetails.Add(newOrderDetail);

                                        tempDetail.OD_QTY = quantity;
                                        tempDetail.OD_AAMT = tempDetail.OD_APRICE * quantity;
                                        tempDetail.OD_SAMT = tempDetail.OD_SPRICE * quantity;
                                        tempDetail.OD_NOTE = notes;
                                        tempDetail.OD_ITG = points;
                                        tempDetail.OD_WEIGHT = weight;
                                        toUpdateOrderDetails.Add(tempDetail);

                                    }
                                }
                            }
                        }

                        ret = new BLL.ORDERDETAIL().UpdateOrderDetailAndOrderMain(false, order.ORDER_ID, DeletedOrderDetailID, newOrderDetails, toUpdateOrderDetails, Share.UserValue(0), ConfigurationManager.AppSettings["ckdm"].ToString(), ConfigurationManager.AppSettings["kwdm"].ToString(), order.ORDER_TRANERCODE, arrMainInfo[2]);

                    }
                }
                ret = Deliver(order.ORDER_ID, Share.UserValue(0), false, "",pc.PCH_ID);


                if (ret)
                {
                    result = "出库成功|Send";
                }
                else
                {
                    result = "出库失败|Send";
                }
            }
        }
    }

    public bool Deliver(string orderId, string operatorId, bool IsUpdateDeliverCode, string deliverCode,string pch)
    {

        int pageCount, recordCount;
        Model.ORDERDOC order = new BLL.ORDERDOC().GetModel(orderId);
        List<Model.ORDERDETAIL> orderDetails = new BLL.ORDERDETAIL().GetList(100, 1, " OD_DETAILID ", false, " and OD_ID='" + orderId + "' ", out pageCount, out recordCount);
        IList<Model.PAYDETAIL> payDetails = new SQLServerDAL.PAYDETAIL().GetPayDetailById(orderId);

        #region 非货到付款流程



        if (order.ORDER_APAY == 0)
        {
            #region 含担保交易



            if (order.ORDER_SECDEAL == 1)
            {
                StringBuilder strSqlToUpdateBStyleDB = new StringBuilder();
                StringBuilder strSqlToUpdateErpDB = new StringBuilder();

                Model.V_VIPSET userInfo = new BLL.V_VIPSET().GetModel(order.ORDER_CUSTOMERCODE);

                Model.V_VIP_ACCOUNT userAccount = new BLL.V_VIP_ACCOUNT().GetModel(userInfo.DM);

                if (userAccount == null)
                {
                    strSqlToUpdateBStyleDB.Append("insert into v_vip_account(DM,DQJE_1) values('");
                    strSqlToUpdateBStyleDB.Append(userInfo.DM);
                    strSqlToUpdateBStyleDB.Append("',0)|");

                    strSqlToUpdateErpDB.Append("insert into v_vip_account(DM,DQJE_1) values('");
                    strSqlToUpdateErpDB.Append(userInfo.DM);
                    strSqlToUpdateErpDB.Append("',0)|");

                }
                if (IsUpdateDeliverCode)
                {
                    strSqlToUpdateBStyleDB.Append("update ORDERDOC set ORDER_SEND=1,ORDER_SENDER='");
                    strSqlToUpdateBStyleDB.Append(operatorId);
                    strSqlToUpdateBStyleDB.Append("',ORDER_SENDDATE='");
                    strSqlToUpdateBStyleDB.Append(DateTime.Now);
                    strSqlToUpdateBStyleDB.Append("',ORDER_TRANBILLCODE='");
                    strSqlToUpdateBStyleDB.Append(deliverCode);
                    strSqlToUpdateBStyleDB.Append("',BYZD21='");
                    strSqlToUpdateBStyleDB.Append(pch);
                    strSqlToUpdateBStyleDB.Append("' where ORDER_ID='");
                    strSqlToUpdateBStyleDB.Append(orderId);
                    strSqlToUpdateBStyleDB.Append("'|");
                }
                else
                {
                    strSqlToUpdateBStyleDB.Append("update ORDERDOC set ORDER_SEND=1,ORDER_SENDER='");
                    strSqlToUpdateBStyleDB.Append(operatorId);
                    strSqlToUpdateBStyleDB.Append("',ORDER_SENDDATE='");
                    strSqlToUpdateBStyleDB.Append(DateTime.Now);
                    strSqlToUpdateBStyleDB.Append("',BYZD21='");
                    strSqlToUpdateBStyleDB.Append(pch);
                    strSqlToUpdateBStyleDB.Append("' where ORDER_ID='");
                    strSqlToUpdateBStyleDB.Append(orderId);
                    strSqlToUpdateBStyleDB.Append("'|");
                }

                for (int i = 0; i < orderDetails.Count; i++)
                {
                    strSqlToUpdateBStyleDB.Append("update SPKCB set SL=SL-");
                    strSqlToUpdateBStyleDB.Append(orderDetails[i].OD_QTY);
                    strSqlToUpdateBStyleDB.Append(" where CKDM='");
                    strSqlToUpdateBStyleDB.Append(ConfigurationManager.AppSettings["ckdm"].ToString());
                    strSqlToUpdateBStyleDB.Append("' and KWDM='");
                    strSqlToUpdateBStyleDB.Append(ConfigurationManager.AppSettings["kwdm"].ToString());
                    strSqlToUpdateBStyleDB.Append("' and SPDM='");
                    strSqlToUpdateBStyleDB.Append(orderDetails[i].OD_CMDCODE);
                    strSqlToUpdateBStyleDB.Append("' and GG1DM='");
                    strSqlToUpdateBStyleDB.Append(orderDetails[i].OD_COLORCODE);
                    strSqlToUpdateBStyleDB.Append("' and GG2DM='");
                    strSqlToUpdateBStyleDB.Append(orderDetails[i].OD_SIZECODE);
                    strSqlToUpdateBStyleDB.Append("'|");
                }



                if (strSqlToUpdateErpDB.ToString() == "")
                {
                    string stringSql = strSqlToUpdateBStyleDB.ToString();
                    char[] charToTrim = new char[] { '|' };
                    string[] sqlCommandArray = stringSql.TrimEnd(charToTrim).Split(charToTrim);
                    return new SQLServerDAL.ORDERDOC().Deliver(sqlCommandArray);
                }
                else
                {
                    string stringSqlToUpdateBStyleDB = strSqlToUpdateBStyleDB.ToString();
                    string stringSqlToUpdateErpDB = strSqlToUpdateErpDB.ToString();

                    char[] charToTrim = new char[] { '|' };
                    string[] strArray1 = stringSqlToUpdateBStyleDB.TrimEnd(charToTrim).Split(charToTrim);
                    string[] strArray2 = stringSqlToUpdateErpDB.TrimEnd(charToTrim).Split(charToTrim);

                    LX_Manage.ERP.Service webService = new LX_Manage.ERP.Service();
                    return webService.UpdateBalanceAndUsableDeliverAmountOfOneRecord(ConfigurationManager.AppSettings["WebServiceKey"].ToString(), strArray1, strArray2);
                }



            }
            #endregion
            #region 不含担保交易
            else if (order.ORDER_SECDEAL == 0)
            {
                StringBuilder strSqlToUpdateBStyleDB = new StringBuilder();
                StringBuilder strSqlToUpdateErpDB = new StringBuilder();

                Model.V_VIPSET userInfo = new BLL.V_VIPSET().GetModel(order.ORDER_CUSTOMERCODE);

                Model.V_VIP_ACCOUNT userAccount = new BLL.V_VIP_ACCOUNT().GetModel(userInfo.DM);

                if (userAccount == null)
                {
                    strSqlToUpdateBStyleDB.Append("insert into v_vip_account(DM,DQJE_1) values('");
                    strSqlToUpdateBStyleDB.Append(userInfo.DM);
                    strSqlToUpdateBStyleDB.Append("',0)|");

                    strSqlToUpdateErpDB.Append("insert into v_vip_account(DM,DQJE_1) values('");
                    strSqlToUpdateErpDB.Append(userInfo.DM);
                    strSqlToUpdateErpDB.Append("',0)|");
                }

                if (IsUpdateDeliverCode)
                {
                    strSqlToUpdateBStyleDB.Append("update ORDERDOC set ORDER_SEND=1,ORDER_SENDER='");
                    strSqlToUpdateBStyleDB.Append(operatorId);
                    strSqlToUpdateBStyleDB.Append("',ORDER_SENDDATE='");
                    strSqlToUpdateBStyleDB.Append(DateTime.Now);
                    strSqlToUpdateBStyleDB.Append("',ORDER_TRANBILLCODE='");
                    strSqlToUpdateBStyleDB.Append(deliverCode);
                    strSqlToUpdateBStyleDB.Append("',BYZD21='");
                    strSqlToUpdateBStyleDB.Append(pch);
                    strSqlToUpdateBStyleDB.Append("' where ORDER_ID='");
                    strSqlToUpdateBStyleDB.Append(orderId);
                    strSqlToUpdateBStyleDB.Append("'|");
                }
                else
                {
                    strSqlToUpdateBStyleDB.Append("update ORDERDOC set ORDER_SEND=1,ORDER_SENDER='");
                    strSqlToUpdateBStyleDB.Append(operatorId);
                    strSqlToUpdateBStyleDB.Append("',ORDER_SENDDATE='");
                    strSqlToUpdateBStyleDB.Append(DateTime.Now);
                    strSqlToUpdateBStyleDB.Append("',BYZD21='");
                    strSqlToUpdateBStyleDB.Append(pch);
                    strSqlToUpdateBStyleDB.Append("' where ORDER_ID='");
                    strSqlToUpdateBStyleDB.Append(orderId);
                    strSqlToUpdateBStyleDB.Append("'|");
                }
                for (int i = 0; i < orderDetails.Count; i++)
                {
                    strSqlToUpdateBStyleDB.Append("update SPKCB set SL=SL-");
                    strSqlToUpdateBStyleDB.Append(orderDetails[i].OD_QTY);
                    strSqlToUpdateBStyleDB.Append(" where CKDM='");
                    strSqlToUpdateBStyleDB.Append(ConfigurationManager.AppSettings["ckdm"].ToString());
                    strSqlToUpdateBStyleDB.Append("' and KWDM='");
                    strSqlToUpdateBStyleDB.Append(ConfigurationManager.AppSettings["kwdm"].ToString());
                    strSqlToUpdateBStyleDB.Append("' and SPDM='");
                    strSqlToUpdateBStyleDB.Append(orderDetails[i].OD_CMDCODE);
                    strSqlToUpdateBStyleDB.Append("' and GG1DM='");
                    strSqlToUpdateBStyleDB.Append(orderDetails[i].OD_COLORCODE);
                    strSqlToUpdateBStyleDB.Append("' and GG2DM='");
                    strSqlToUpdateBStyleDB.Append(orderDetails[i].OD_SIZECODE);
                    strSqlToUpdateBStyleDB.Append("'|");
                }

                decimal points = Convert.ToDecimal(0.00);
                foreach (Model.ORDERDETAIL temp in orderDetails)
                {
                    points += temp.OD_ADDITG;
                }

                string strUpdateVipSetSqlString = new BLL.V_VIPSET().BuildUpdateVipSetSqlString(0, 0, 0, order.ORDER_CUSTOMERCODE);
                strSqlToUpdateBStyleDB.Append(strUpdateVipSetSqlString);
                strSqlToUpdateErpDB.Append(strUpdateVipSetSqlString);

                if (order.ORDER_PAYAMT - order.ORDER_AAMT > 0)
                {
                    string strUpdateVipAccountSqlString = new BLL.V_VIP_ACCOUNT().BuildUpdateVipAccountSqlString(order.ORDER_PAYAMT - order.ORDER_AAMT, userInfo.DM);
                    strSqlToUpdateBStyleDB.Append(strUpdateVipAccountSqlString);
                    strSqlToUpdateErpDB.Append(strUpdateVipAccountSqlString);

                    string strInsertBalanceManagementSqlString = new BLL.BALANCEADJUST().BuildInsertMainSqlString("005", order.ORDER_ID, operatorId, operatorId, order.ORDER_CUSTOMERCODE, order.ORDER_PAYAMT - order.ORDER_AAMT);
                    strSqlToUpdateBStyleDB.Append(strInsertBalanceManagementSqlString);

                }

                string stringSqlToUpdateBStyleDB = strSqlToUpdateBStyleDB.ToString();
                string stringSqlToUpdateErpDB = strSqlToUpdateErpDB.ToString();

                char[] charToTrim = new char[] { '|' };
                string[] strArray1 = stringSqlToUpdateBStyleDB.TrimEnd(charToTrim).Split(charToTrim);
                string[] strArray2 = stringSqlToUpdateErpDB.TrimEnd(charToTrim).Split(charToTrim);

                LX_Manage.ERP.Service webService = new LX_Manage.ERP.Service();
                return webService.UpdateBalanceAndUsableDeliverAmountOfOneRecord(ConfigurationManager.AppSettings["WebServiceKey"].ToString(), strArray1, strArray2);

            }
            #endregion
        }
        #endregion
        #region 货到付款流程
        else if (order.ORDER_APAY == 1)
        {
            Model.V_VIPSET userInfo = new BLL.V_VIPSET().GetModel(order.ORDER_CUSTOMERCODE);

            Model.V_VIP_ACCOUNT userAccount = new BLL.V_VIP_ACCOUNT().GetModel(userInfo.DM);

            StringBuilder strSqlToUpdateBStyleDB = new StringBuilder();
            StringBuilder strSqlToUpdateErpDB = new StringBuilder();

            if (userAccount == null)
            {
                strSqlToUpdateBStyleDB.Append("insert into v_vip_account(DM,DQJE_1) values('");
                strSqlToUpdateBStyleDB.Append(userInfo.DM);
                strSqlToUpdateBStyleDB.Append("',0)|");

                strSqlToUpdateErpDB.Append("insert into v_vip_account(DM,DQJE_1) values('");
                strSqlToUpdateErpDB.Append(userInfo.DM);
                strSqlToUpdateErpDB.Append("',0)|");
            }

            if (IsUpdateDeliverCode)
            {
                strSqlToUpdateBStyleDB.Append("update ORDERDOC set ORDER_SEND=1,ORDER_SENDER='");
                strSqlToUpdateBStyleDB.Append(operatorId);
                strSqlToUpdateBStyleDB.Append("',ORDER_SENDDATE='");
                strSqlToUpdateBStyleDB.Append(DateTime.Now);
                strSqlToUpdateBStyleDB.Append("',ORDER_TRANBILLCODE='");
                strSqlToUpdateBStyleDB.Append(deliverCode);
                strSqlToUpdateBStyleDB.Append("',BYZD21='");
                strSqlToUpdateBStyleDB.Append(pch);
                strSqlToUpdateBStyleDB.Append("' where ORDER_ID='");
                strSqlToUpdateBStyleDB.Append(orderId);
                strSqlToUpdateBStyleDB.Append("'|");
            }
            else
            {
                strSqlToUpdateBStyleDB.Append("update ORDERDOC set ORDER_SEND=1,ORDER_SENDER='");
                strSqlToUpdateBStyleDB.Append(operatorId);
                strSqlToUpdateBStyleDB.Append("',ORDER_SENDDATE='");
                strSqlToUpdateBStyleDB.Append(DateTime.Now);
                strSqlToUpdateBStyleDB.Append("',BYZD21='");
                strSqlToUpdateBStyleDB.Append(pch);
                strSqlToUpdateBStyleDB.Append("' where ORDER_ID='");
                strSqlToUpdateBStyleDB.Append(orderId);
                strSqlToUpdateBStyleDB.Append("'|");
            }

            for (int i = 0; i < orderDetails.Count; i++)
            {
                strSqlToUpdateBStyleDB.Append("update SPKCB set SL=SL-");
                strSqlToUpdateBStyleDB.Append(orderDetails[i].OD_QTY);
                strSqlToUpdateBStyleDB.Append(" where CKDM='");
                strSqlToUpdateBStyleDB.Append(ConfigurationManager.AppSettings["ckdm"].ToString());
                strSqlToUpdateBStyleDB.Append("' and KWDM='");
                strSqlToUpdateBStyleDB.Append(ConfigurationManager.AppSettings["kwdm"].ToString());
                strSqlToUpdateBStyleDB.Append("' and SPDM='");
                strSqlToUpdateBStyleDB.Append(orderDetails[i].OD_CMDCODE);
                strSqlToUpdateBStyleDB.Append("' and GG1DM='");
                strSqlToUpdateBStyleDB.Append(orderDetails[i].OD_COLORCODE);
                strSqlToUpdateBStyleDB.Append("' and GG2DM='");
                strSqlToUpdateBStyleDB.Append(orderDetails[i].OD_SIZECODE);
                strSqlToUpdateBStyleDB.Append("'|");
            }

            if (strSqlToUpdateErpDB.ToString() == "")
            {
                string stringSql = strSqlToUpdateBStyleDB.ToString();
                char[] charToTrim = new char[] { '|' };
                string[] sqlCommandArray = stringSql.TrimEnd(charToTrim).Split(charToTrim);
                return new SQLServerDAL.ORDERDOC().Deliver(sqlCommandArray);
            }
            else
            {
                string stringSqlToUpdateBStyleDB = strSqlToUpdateBStyleDB.ToString();
                string stringSqlToUpdateErpDB = strSqlToUpdateErpDB.ToString();

                char[] charToTrim = new char[] { '|' };
                string[] strArray1 = stringSqlToUpdateBStyleDB.TrimEnd(charToTrim).Split(charToTrim);
                string[] strArray2 = stringSqlToUpdateErpDB.TrimEnd(charToTrim).Split(charToTrim);

                LX_Manage.ERP.Service webService = new LX_Manage.ERP.Service();
                return webService.UpdateBalanceAndUsableDeliverAmountOfOneRecord(ConfigurationManager.AppSettings["WebServiceKey"].ToString(), strArray1, strArray2);
            }
        }
        #endregion

        else
        {
            return false;
        }
        return false;

    }

    #endregion
}
